package cn.lili.modules.promotion.serviceimpl;

import cn.lili.common.utils.StringUtils;
import cn.lili.modules.promotion.entity.dos.Ticket;
import cn.lili.modules.promotion.entity.dos.TicketArea;
import cn.lili.modules.promotion.entity.dos.TicketOrder;
import cn.lili.modules.promotion.entity.enums.TicketOrderStatusEnum;
import cn.lili.modules.promotion.entity.enums.TicketSaleStateEnum;
import cn.lili.modules.promotion.entity.vos.TicketAreaStatisticsVO;
import cn.lili.modules.promotion.entity.vos.TicketStatisticsVO;
import cn.lili.modules.promotion.mapper.TicketOrderMapper;
import cn.lili.modules.promotion.service.TicketAreaService;
import cn.lili.modules.promotion.service.TicketService;
import cn.lili.modules.promotion.service.TicketStatisticsService;
import cn.lili.modules.statistics.entity.enums.SearchTypeEnum;
import cn.lili.modules.statistics.util.StatisticsDateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@RequiredArgsConstructor
public class TicketStatisticsServiceImpl implements TicketStatisticsService {
    private final TicketService ticketService;

    @Resource
    private TicketOrderMapper ticketOrderMapper;

    private final TicketAreaService ticketAreaService;

    /**
     * 工作台统计门票销售数据
     * @return
     */
    @Override
    public TicketStatisticsVO getTicketStatistics () {
        TicketStatisticsVO ticketStatisticsVO = new TicketStatisticsVO();
        List<TicketAreaStatisticsVO> list = Lists.newArrayList();
        // 获取门票总数
        long ticketTotalCount = ticketService.count(new LambdaQueryWrapper<Ticket>().eq(Ticket::getDeleteFlag, 0));
        ticketStatisticsVO.setTotalCount(ticketTotalCount);
        // 统计各区域门票总数
        List<TicketArea> areaList = ticketAreaService.getList();
        if (!areaList.isEmpty()) {
            areaList.forEach(area -> {
                long areaTicketCount = getAreaTicketCountByArea(area.getId());
                TicketAreaStatisticsVO ticketAreaStatisticsVO = new TicketAreaStatisticsVO();
                ticketAreaStatisticsVO.setArea(area.getAreaName());
                ticketAreaStatisticsVO.setAreaCount(areaTicketCount);
                // 获取区域在售状态票数
                long onSaleCount = getStatisticsByArea(area.getId(), TicketSaleStateEnum.ONSALE.name(), 1);
                ticketAreaStatisticsVO.setOnSaleCount(onSaleCount);
                // 获取区域已售状态票数
                long ofSaleCount = getStatisticsByArea(area.getId(), TicketSaleStateEnum.OFFSALE.name(), null);
                ticketAreaStatisticsVO.setOfSaleCount(ofSaleCount);
                // 获取区域下架状态票数
                long downCount = getStatisticsByArea(area.getId(), null, 0);
                ticketAreaStatisticsVO.setDownCount(downCount);
                list.add(ticketAreaStatisticsVO);
            });
            ticketStatisticsVO.setTicketAreaStatisticsVOList(list);
        }
        return ticketStatisticsVO;
    }


    /**
     * 统计各区域今天门票销售数量
     * @return
     */
    @Override
    public List<TicketAreaStatisticsVO> getTodayStatistics () {
        List<TicketAreaStatisticsVO> list = new ArrayList<>();
        Date[] dates = StatisticsDateUtil.getDateArray(SearchTypeEnum.TODAY);
        List<TicketArea> areaList = ticketAreaService.getList();
        if (!areaList.isEmpty()) {
            areaList.forEach(area -> {
                TicketAreaStatisticsVO ticketAreaStatisticsVO = new TicketAreaStatisticsVO();
                ticketAreaStatisticsVO.setArea(area.getAreaName());
                QueryWrapper<TicketOrder> queryWrapper = new QueryWrapper<>();
                queryWrapper.between("lto.place_time", dates[0], dates[1]);
                queryWrapper.eq("lt.sale_state", TicketSaleStateEnum.OFFSALE.name());
                queryWrapper.eq("lt.area_id", area.getId());
                long count = ticketOrderMapper.getTodayStatisticsByArea(queryWrapper);
                ticketAreaStatisticsVO.setOfSaleCount(count);
                list.add(ticketAreaStatisticsVO);
            });
        }
        return list;
    }

    /**
     * 获取区域和区域总票数
     *
     * @return
     */
    public long getAreaTicketCountByArea(String areaId) {
        LambdaQueryWrapper<Ticket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Ticket::getAreaId, areaId);
        queryWrapper.eq(Ticket::getDeleteFlag, 0);
        return ticketService.count(queryWrapper);
    }

    /**
     * 根据区域统计区域在售、已售、下架票数
     *
     * @param area      区域
     * @param saleState 销售状态
     * @param downState 上下架状态
     * @return
     */
    public long getStatisticsByArea(String area, String saleState, Integer downState) {
        LambdaQueryWrapper<Ticket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Ticket::getDeleteFlag, 0);
        queryWrapper.eq(StringUtils.isNotEmpty(area), Ticket::getAreaId, area);
        queryWrapper.eq(StringUtils.isNotEmpty(saleState), Ticket::getSaleState, saleState);
        queryWrapper.eq(null != downState, Ticket::getDownState, downState);
        return ticketService.count(queryWrapper);
    }
}
